<template>
  <div class="add-form">
    <el-page-header @back="$router.back()" :content="`${form.id ? '编辑':'添加'}`">
    </el-page-header>
    <el-form ref="form" :model="form" :rules="rules" label-width="100px" style="margin-top: 20px;">
      <el-form-item label="经济商" prop="broker_id">
        <BrokerSelect v-model="form.broker_id"></BrokerSelect>
      </el-form-item>

      <el-form-item label="服务商名称" prop="name">
        <el-input v-model="form.name"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">提交</el-button>
      </el-form-item>

    </el-form>

  </div>
</template>

<script>
  import BrokerSelect from "./components/BrokerSelect.vue"
  import {
    ajaxPost
  } from '@/api/ajax'
  export default {
    components: {
      BrokerSelect
    },
    data() {
      return {
        form: {
          id: '',
          broker_id: '',
          name: ''
        },
        rules: {
          broker_id: [{
            required: true,
            message: '请选择经济商',
            trigger: 'blur'
          }, ],
          name: [{
            required: true,
            message: '请输入服务商名称',
            trigger: 'blur'
          }, ],
        },
        row: [],
      }
    },
    created() {
      if (this.$route.params.id) {
        this.form = this.$route.params;

      }
    },
    methods: {
      onSubmit() {
        this.$refs.form.validate((valid) => {
          if (valid) {
            let data = {
              ...this.form
            };
            if (data.id) {
              ajaxPost('server/update', data).then(res => {
                if (res.code == 0) {
                  this.$message({
                    message: '编辑成功',
                    type: "success",
                    duration: 1500,
                    onClose: () => {
                      this.$router.back();
                    },
                  })
                }
              })
            } else {
              ajaxPost('server/create', data).then(res => {
                if (res.code == 0) {
                  this.$message({
                    message: '添加成功',
                    type: "success",
                    duration: 1500,
                    onClose: () => {
                      this.$router.back();
                    },
                  })
                }
              });
            }
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      }
    }
  }
</script>

<style>
  .add-form {
    padding: 20px;
    box-sizing: border-box;
    /* input {
      width: 500px;
    } */
  }
</style>
